define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var util = require("./util/util");
	var Model = function() {
		this.callParent();
		this.orderbyValue = [ {
			index : 0,
			key : "com.qq.weixin.miniapp.appID"
		}, {
			index : 1,
			key : "com.qq.weixin.miniapp.appSecret"
		}, {
			index : 2,
			key : "com.qq.weixin.miniapp.token"
		}, {
			index : 3,
			key : "com.qq.weixin.miniapp.encodingAESKey"
		}, {
			index : 4,
			key : "com.qq.weixin.miniapp.notifyUrl"
		}, {
			index : 5,
			key : "com.qq.weixin.miniapp.pay.mchid"
		}, {
			index : 6,
			key : "com.qq.weixin.miniapp.pay.mchKey"
		}, {
			index : 7,
			key : "com.qq.weixin.miniapp.pay.apiClientCertPath"
		}, {
			index : 8,
			key : "com.qq.weixin.miniapp.pay.notifyUrl"
		}, {
			index : 9,
			key : "com.qq.weixin.miniapp.pay.refundNotifyUrl"
		} ];
	};

	Model.prototype.newprogramdialogReceived = function(event) {
		if (!event.data.progaramName)
			return;
		var newData = this.comp("newData");
		var groupValue = "default_" + util.getGroupId();
		newData.newData({
			defaultValues : [ {
				ID : "",
				name : event.data.progaramName,
				fvalue : groupValue
			} ]
		});
		this.comp("selectData").setValue("name", event.data.progaramName);
		this.comp("selectData").setValue("fvalue", groupValue);
		this.comp("wxprogramData").clear();
		this.wxPublicDataDefault(groupValue, event.data.progaramName);
		this.comp("select1").set({
			disabled : true
		});
		this.comp("operationBtn").set({
			disabled : true
		});
	};

	Model.prototype.button1Click = function(event) {
		var self = this;
		if (!self.comp("newData").val("fvalue")) {
			this.comp("newprogramdialog").open();
		} else {
			$.ajax({
				url : "/config/config/configs/search/like",
				type : "GET",
				data : {
					"key" : "com.qq.weixin.miniapp",
					"group" : self.comp("newData").val("fvalue")
				},
				success : function(res) {
					if (res.length === 0) {
						justep.Util.hint("当前小程序未保存，请保存后再新增！", {
							"type" : "warning"
						});
					} else {
						self.comp("newprogramdialog").open({
							data : self.comp("newData").toJson({
								"format" : "simple"
							}).rows
						});
					}
				},
				error : function(err) {
					justep.Util.hint("网络错误，请稍后再试", {
							"type" : "warning"
						});
				}
			});

		}
	};

	Model.prototype.selectDataValueChanged = function(event) {
	};

	Model.prototype.select1Change = function(event) {
		var self = this;
		$.ajax({
			url : "/config/config/configs/search/like",
			type : "GET",
			data : {
				"key" : "com.qq.weixin.miniapp",
				"group" : event.value
			},
			success : function(res) {
				if (res.length === 0) {
				} else {
					res.push({
						"id" : "",
						"fkey" : "com.qq.weixin.miniapp.orderby",
						"fvalue" : self.orderbyValue,
						"fgroup" : "default",
						"flabel" : "排序",
						issecure : 0
					});

					res = util.sort(res, "com.qq.weixin.miniapp.orderby");
					self.comp("wxprogramData").clear();
					self.comp("wxprogramData").loadData(res);
					self.comp("wxprogramData").refreshData();
				}
			},
			error : function(err) {
			}
		});
	};

	Model.prototype.button2Click = function(event) {

	};

	Model.prototype.saveBtnClick = function(event) {
		var wxprogramData = this.comp("wxprogramData");
		var option = wxprogramData.toJson({
			"format" : "simple"
		}).rows;
		var self = this;
		option.forEach(function(item, index) {
			delete item.userdata;
			if(item.fvalue && typeof(item.fvalue)!="object"){
			    item.fvalue.trim();
			}
		});
		$.ajax({
			url : "/config/config/config",
			type : "POST",
			contentType : "application/json",
			datatype : "json",
			data : JSON.stringify(option),
			success : function(res) {
				justep.Util.hint("保存成功");
				self.comp("wxprogramData").loadData(res);
				res.forEach(function(item,index){
					if(item.fkey == "com.qq.weixin.miniapp.group"){
						self.comp("newData").setValue("id",item.id);
					}
				});
				self.comp("select1").set({
					disabled : false
				});
				self.comp("operationBtn").set({
					disabled : false
				});
			},
			error : function(err) {
				justep.Util.hint("保存失败，请稍后重试！", {
					"type" : "warning"
				});
			}
		});
	};
	Model.prototype.wxPublicDataDefault = function(groupName, groupLabel) {

		var wxprogramData = this.comp("wxprogramData");
		var options = [ {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.appID",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "appID",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.appSecret",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "appSecret",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.token",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "消息事件消息加解密token",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.encodingAESKey",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "消息事件消息加解密encodingAESKey",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.notifyUrl",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "微信推送消息解密后通知地址",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.pay.mchid",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "小程序匹配对应的商户号",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.pay.mchKey",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "小程序匹配对应的商户秘钥",
			issecure : 0

		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.pay.apiClientCertPath",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "小程序匹配对应的商户证书地址",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.pay.notifyUrl",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "支付成功通知地址",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.pay.refundNotifyUrl",
			"fvalue" : '',
			"fgroup" : groupName,
			"flabel" : "退款成功通知地址",
			issecure : 0
		}, {
			"id" : "",
			"fkey" : "com.qq.weixin.miniapp.group",
			"fvalue" : groupName,
			"fgroup" : "default_group",
			"flabel" : groupLabel,
			issecure : 0
		}];
		wxprogramData.newData({
			"defaultValues" : options
		});
	};
	Model.prototype.groupRequest = function() {
		return new Promise(function(resolve, reject) {
			$.ajax({
				url : "/config/config/configs/search/like",
				type : "GET",
				data : {
					"key" : "com.qq.weixin.miniapp",
					"group" : "default_group"
				},
				success : function(res) {
					resolve(res);
				},
				error : function(err) {
					reject(err);
				}
			});
		});
	};

	Model.prototype.newDataAfterRefresh = function(event) {
		var self = this;
		var data = this.comp("newData");
		this.groupRequest().then(function(result) {
			var groupData = [];
			result.forEach(function(item, index) {
				var obj = {};
				obj.id = item.id;
				obj.name = item.flabel;
				obj.fvalue = item.fvalue;
				groupData.push(obj);
			});
			data.loadData(groupData);
			var row = data.getFirstRow();
			if (row) {
				self.comp("selectData").setValue("name", row.val("name"));
				self.comp("selectData").setValue("fvalue", row.val("fvalue"));
				$.ajax({
					url : "/config/config/configs/search/like",
					type : "GET",
					data : {
						"key" : "com.qq.weixin.miniapp",
						"group" : row.val("fvalue")
					},
					success : function(res) {
						if (res.length === 0) {
						} else {
							res.push({
								"id" : "",
								"fkey" : "com.qq.weixin.miniapp.orderby",
								"fvalue" : self.orderbyValue,
								"fgroup" : "default",
								"flabel" : "排序",
								issecure : 0
							});
							res = util.sort(res, "com.qq.weixin.miniapp.orderby");
							self.comp("wxprogramData").loadData(res);
						}
					},
					error : function(err) {
					}
				});
			}
		});
	};

	Model.prototype.wxprogramDataCustomRefresh = function(event) {

	};

	Model.prototype.operationBtnClick = function(event) {
		var self = this;
		this.comp("programOperationDilaog").open({
			data : self.comp("newData").toJson({
				"format" : "simple"
			}).rows
		});
	};

	Model.prototype.programOperationDilaogReceived = function(event) {
		this.getParent().comp("programContainer").refresh();
	};

	return Model;
});